package com.itheima.web.manager;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.itheima.domain.db.Admin;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.db.AdminService;
import com.itheima.service.mongo.MovementService;
import com.itheima.utils.ConstantUtil;
import com.itheima.utils.JwtUtil;
import com.itheima.web.exception.BusinessException;
import com.itheima.web.interceptor.AdminHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import sun.plugin2.message.Message;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Guanxing
 * @date 2021/9/30
 */
@Component
public class AdminManager {

    @Reference
    private AdminService adminService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //生成验证码
    public LineCaptcha createCode(String uuid) {
        //生成图片
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100, 4, 150);

        //将图片生成的验证码存入redis
        String code = lineCaptcha.getCode();
        System.out.println("验证码 " + code);
        stringRedisTemplate.opsForValue().set(ConstantUtil.ADMIN_CODE + uuid, code, Duration.ofMinutes(5));

        return lineCaptcha;
    }

    //登录
    public String login(String username, String password, String verificationCode, String uuid) throws BusinessException {
        //校验验证码
        String redisCode = stringRedisTemplate.opsForValue().get(ConstantUtil.ADMIN_CODE + uuid);
        //如果校验失败,抛出业务异常
        if (!StrUtil.equals(verificationCode, redisCode)) {
            throw new BusinessException("验证码错误");
        }

        //若校验成功,根据用户名查询
        Admin admin = adminService.findByUsername(username);
        if (admin == null) {
            throw new BusinessException("用户名错误");
        }

        //查询到用户之后校验密码
        String md5Pwd = SecureUtil.md5(password);
        System.out.println(md5Pwd);
        //若密码比较失败,抛出业务异常
        if (!StrUtil.equals(md5Pwd, admin.getPassword())) {
            throw new BusinessException("密码输入错误");
        }

        //密码校验成功,生成token
        Map map = new HashMap();
        map.put("id", admin.getId());
        map.put("usernam", username);
        String token = JwtUtil.createToken(map);

        //将数据存入redis
        stringRedisTemplate.delete(ConstantUtil.ADMIN_CODE);
        stringRedisTemplate.opsForValue().set(ConstantUtil.ADMIN_TOKEN + token, "1", Duration.ofMinutes(30));

        return token;
    }

    //查询用户请求
    public Admin findAminInfo(String token) {
        /*//去掉自动携带的前缀
        token = token.replace("Bearer ", "");
        //解析token
        Map map = JwtUtil.parseToken(token);

        Admin admin = BeanUtil.mapToBean(map, Admin.class, true);//这个admin只有id和用户名*/

        return adminService.findById(AdminHolder.getId());

    }

    //登出
    public void logout(String token) {
        //去掉自动携带的前缀
        token = token.replace("Bearer ", "");

        stringRedisTemplate.delete(ConstantUtil.ADMIN_TOKEN + token);
    }

    @Reference
    private MovementService movementService;

    //动态复审-通过
    public Message messagesPass(String[] ids) {

        for (String id : ids) {
            Movement movement = movementService.findMovementByMovementId(id);
            movement.setState(1);
            movementService.updateMovement(movement);
        }
        return null;
    }

    //动态复审-拒绝
    public Message messagesReject(String[] ids) {
        for (String id : ids) {
            Movement movement = movementService.findMovementByMovementId(id);
            movement.setState(2);
            movementService.updateMovement(movement);
        }
        return null;
    }
}
